<template>
  <div style="padding: 10px">
    <el-table :data="tableData"   stripe style="width: 100%">
      <el-table-column type="index" width="50"  label="序号"/>
      <!--      <el-table-column fixed prop="id" sortable label="ID" width="150"/>-->
      <el-table-column label="订单号" width="240">
        <template #default="scope" >
          <el-button type="text" @click="getOrderDetail(scope.row)"
          >{{scope.row.orderId}}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column  label="创建时间" prop="createTime" width="240"/>

      <el-table-column label="订单状态" width="120" >
        <template #default="scope">
          <view v-if="scope.row.flag===0"><el-button type="info"  >待处理</el-button></view>
          <view v-if="scope.row.flag===1"><el-button type="success"  >已发货</el-button></view>
          <view v-if="scope.row.flag===2"><el-button type="warning"  >暂存（待补发货）</el-button></view>
          <view v-if="scope.row.flag===3"><el-button type="danger"  >已退货</el-button></view>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="240" style="align-self: center">
        <template #default="scope">
          <view v-if="scope.row.flag===2"><el-button type="primary" @click="compare(scope.row)" >库存对比</el-button></view>
          &nbsp;
          &nbsp;
          &nbsp;
          <!--          <view v-if="scope.row.flag===0"><el-button type="primary"  >发货</el-button></view>-->

        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" title="订单详情" width="50%">
      <el-table :data="tableData2"   stripe style="width: 100%">
        <el-table-column label="订单号" width="240">
          <template #default="scope" >
            <el-button type="text" @click="dialogFormVisible = true"
            >{{scope.row.orderId}}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="carPartsName" label="汽车配件名称" width="120"/>
        <el-table-column prop="standard" label="规格" width="120"/>
        <el-table-column prop="carNumber" label="配件编号" width="120"/>
        <!--      <el-table-column prop="buy_price" label="进价" width="120"/>-->
        <el-table-column prop="price" label="价格" width="120"/>
        <el-table-column prop="buy_number" label="购买数量" width="120"/>

      </el-table>
    </el-dialog>
    <el-dialog v-model="dialogFormVisible2" title="库存对比" width="50%">
      <el-table :data="tableData2"   stripe style="width: 100%">
        <el-table-column prop="carPartsName" label="汽车配件名称" width="120"/>
        <el-table-column prop="buy_number" label="购买数量" width="120"/>
        <el-table-column prop="have_number" label="未发数量" width="120"/>

        <el-table-column  label="剩余数量" width="240">
          <template #default="scope">
            {{this.tableData3[scope.row.carNumber]}}
            &nbsp;
            &nbsp;
            &nbsp;
            <el-button v-if="scope.row.have_number<=this.tableData3[scope.row.carNumber]" type="success">库存充足</el-button>
            <el-button v-if="scope.row.have_number>this.tableData3[scope.row.carNumber]" type="warning">库存不足</el-button>
            <el-button v-if="this.tableData3[scope.row.carNumber]===0" type="danger">0库存</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button  type="primary" @click="chang_order_flag()">补发货</el-button>
    </el-dialog>
  </div>

</template>

<script>
import request from "@/utils/request";

export default {
  name: "TSOrder",
  data(){
    let sum_price=0;
    let userId=JSON.parse(sessionStorage.getItem("user")).id;
    return {
      userId,
      tableData: [],
      tableData2:[],
      tableData3:[],
      sum_price,
      dialogFormVisible:false,
      dialogFormVisible2:false,
      flag:false,
    }
  },
  created() {
    this.load()
  },
  methods:{
    chang_order_flag(){
      // console.log(this.tableData2[0])
      let flag=0;
      let flags=0;//默认库存充足，1为库存不足，2为无库存，无法发货
      for (let i=0;i<this.tableData2.length;i++){
        if(this.tableData3[this.tableData2[i].carNumber]!==0){
          flag=1;//说明有货物可发，否则不可发
        }
      }
      if(flag===0){
        this.$message({
          type:"error",
          message:"一件货物都没有~~~快去催补货吧"
        })
        return
      }
      //但凡有一种货物不足，都进入暂存状态
      for (let i=0;i<this.tableData2.length;i++){
        if(this.tableData2[i].have_number>this.tableData3[this.tableData2[i].carNumber]){
          flags=1;
        }
      }
      for (let i=0;i<this.tableData2.length;i++){
        if(this.tableData2[i].have_number>this.tableData3[this.tableData2[i].carNumber]){
          let sub_number=this.tableData2[i].have_number-this.tableData3[this.tableData2[i].carNumber];
          let number=0;
          request.get("/goods/change_number/"+this.tableData2[i].carNumber+"/"+number+"/"+sub_number);
          request.get("/order_detail/"+this.tableData2[i].id+"/"+sub_number)

        }else{
          let number=this.tableData3[this.tableData2[i].carNumber]-this.tableData2[i].have_number;
          let sub_number=0;
          request.get("/goods/change_number/"+this.tableData2[i].carNumber+"/"+number+"/"+sub_number);
          request.get("/order_detail/"+this.tableData2[i].id+"/"+sub_number)

        }
      }
      request.get("/order_form/order_id/"+this.tableData2[0].orderId).then(res=>{
        let order_form=res.data;
        //改变为发货状态
        if(flags===0){
          order_form.flag=1
        }else {
          //暂存状态
          order_form.flag=2
        }
        request.post("/order_form/save",order_form).then(res=>{
          this.load();
        })

      })
      if(flags===0){
        this.$message({
          type:'success',
          message:"发货成功"
        })
      }else {
        this.$message({
          type:'warning',
          message:"发货成功,但由于库存不足，订单进入暂存状态"
        })
      }


      this.dialogFormVisible2=false

    },
    compare(row){
      request.get("/order_detail/by_orderId/"+row.orderId).then(res=>{
        console.log(res)
        this.tableData2=res.data
      })
      request.get("/goods/get_all_number").then(res=>{
        console.log(res)
        this.tableData3=res.data
      })
      this.dialogFormVisible2 = true
    },
    load() {
      request.get("/order_form/2").then(res => {
        console.log("获取订单列表",res)
        this.tableData = res.data
        for(let i=0;i<this.tableData.length;i++){
          this.tableData[i].createTime=this.tableData[i].createTime.substring(0,19)
          this.tableData[i].createTime=this.tableData[i].createTime.replace("T","--")
        }
      })

    },
    getOrderDetail(row){
      request.get("/order_detail/by_orderId/"+row.orderId).then(res=>{
        console.log(res)
        this.tableData2=res.data
      })
      this.dialogFormVisible = true

    }
  }
}
</script>

<style scoped>

</style>
